package com.lzx.adx.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.adx.commmon.service.impl.CrudServiceImpl;
import com.lzx.adx.report.bean.mapper.ReportMapper;
import com.lzx.adx.report.dao.WphOrderDao;
import com.lzx.adx.report.dto.WphOrderDTO;
import com.lzx.adx.report.entity.WphOrderEntity;
import com.lzx.adx.report.service.WphOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * 
 *
 * @author wangzhijun 825400558@qq.com
 * @since 1.0.0 2023-11-24
 */
@Service
public class WphOrderServiceImpl extends CrudServiceImpl<WphOrderDao, WphOrderEntity, WphOrderDTO> implements WphOrderService {

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public QueryWrapper<WphOrderEntity> getWrapper(Map<String, Object> params){
        String channelTag = (String)params.get("channelTag");
        Integer hand_type = (Integer)params.get("hand_type");
        String orderSn = (String)params.get("orderSn");

        QueryWrapper<WphOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(channelTag), "channel_tag", channelTag);
        wrapper.eq(Objects.nonNull(hand_type), "hand_type", hand_type);
        wrapper.eq(StringUtils.isNotBlank(orderSn), "id", orderSn);

        return wrapper;
    }


    @Override
    public void save(WphOrderDTO dto) {

        WphOrderEntity wphOrderEntity=  reportMapper.toWphOrderEntity(dto);
        wphOrderEntity.setId(dto.getOrderSn());

        this.baseDao.insert(wphOrderEntity);
    }

    @Override
    public void update(WphOrderDTO dto) {

        WphOrderEntity wphOrderEntity=  reportMapper.toWphOrderEntity(dto);
        wphOrderEntity.setId(dto.getOrderSn());

        this.baseDao.updateById(wphOrderEntity);
    }

    @Override
    public void saveData(WphOrderDTO wphOrderDTO) {
        WphOrderEntity wphOrderEntity = this.baseDao.selectById(wphOrderDTO.getOrderSn());
        if(Objects.nonNull(wphOrderEntity)){
            this.update(wphOrderDTO);
        }else{
           this.save(wphOrderDTO);
        }
    }
}